#include <bits/stdc++.h>
using namespace std;

const int N = 5e5+5;
struct node {
    node *prev, *next;
    int v;
};

node head;

void link(node *a, node *b) {
    if (a != NULL) a->next = b;
    if (b != NULL) b->prev = a;
}

node *dat[2][N];

void insertafter(node *n, int v) {
    assert(n != NULL);
    // printf("n=%p, v=%d\n", n, v);
    node *newn = (node*)malloc(sizeof(node));
    newn->prev = newn->next = NULL;
    newn->v = v;
    link(newn, n->next);
    link(n, newn);
    if (v > 0) dat[0][v] = newn;
    else dat[1][-v] = newn;
}


int n;

pair<int, int> a[N];

int f[N];

vector<int> G[N];

void dfs(int u, int fa) {
    if (u != fa) {
        insertafter(dat[1][fa]->prev, u);
        insertafter(dat[1][fa], -u);
    }

    for (int v : G[u]) {
        if (v == fa) continue;
        dfs(v, u);
    }
}

int main(int argc, char const *argv[])
{
    head.next = NULL;
    scanf("%d", &n);
    for (int i=0; i<n-1; ++i) {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    insertafter(&head, 1);
    // return 0;
    insertafter(dat[0][1], -1);
    // return 0;

    dfs(1, 1);

    
    int id = 0;

    

    for (auto p=head.next; p!=NULL; p=p->next) {
        ++id;
        if (p->v > 0) {
            a[p->v].first = id;
        } else a[-p->v].second = id;
    }

    for (int i=1; i<=n; ++i) {
        printf("%d %d\n", a[i].first, a[i].second);
    }

    return 0;
}
